﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;

namespace FYPHP
{
    public partial class prelayout2 : System.Web.UI.UserControl
    {
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["fyphp"].ConnectionString);

        protected void Page_Load(object sender, EventArgs e)
        {
            String bgColor = "#" + Request.QueryString["c"];
            String font = Request.QueryString["f"];

            SqlCommand cmdProduct, cmdCount;
            SqlDataReader dtrProduct;
            conn.Open();

            cmdCount = new SqlCommand("SELECT COUNT(*) FROM Products p, Users u, Category c WHERE p.p_category=c.cat_id AND p.u_id=u.u_id AND p_status='Approve' AND u_status='Activate' AND p.u_id=@id", conn);
            cmdCount.Parameters.AddWithValue("@id", Session["id"]);
            int c = Convert.ToInt32(cmdCount.ExecuteScalar());

            cmdProduct = new SqlCommand("SELECT * FROM Products p, Users u, Category c WHERE p.p_category=c.cat_id AND p.u_id=u.u_id AND p_status='Approve' AND u_status='Activate' AND p.u_id=@id", conn);
            cmdProduct.Parameters.AddWithValue("@id", Session["id"]);
            dtrProduct = cmdProduct.ExecuteReader();

            String thead = "<div style='font-family: " + font + "'><h2>" + Session["Username"] + " Virtual Shop</h2><table class='thumbs' width='100%' cellpadding='5' cellspacing='5' border='1' bordercolor='#aaa' align='center' style='background-color: " + bgColor + "; border-collapse: collapse; text-align: center'>";
            String tbox = "";
            String tfoot = "</tr></table></div>";

            if (c % 3 != 0)
                c = (c / 3) + 1;
            else
                c = (c / 3);

            if (dtrProduct.HasRows)
            {
                for (int i = 0; i < c; i++)
                {
                    for (int j = 0; j < 3; j++)
                    {
                        if (dtrProduct.Read())
                        {
                            tbox += "<td><a href='/ProductDetails.aspx?id=" + dtrProduct["p_id"] + "'><img src='/uploads/products/" + dtrProduct["p_picture"] + "' alt='' height='180px' width='180px' /></a><br />"
                                    + "<a href='/ProductDetails.aspx?id=" + dtrProduct["p_id"] + "'>" + dtrProduct["p_name"] + "</a></td>";
                        }
                    }
                    tbox += "</tr><tr>";
                }
            }
            lblLay2.Text = thead + tbox + tfoot;

            dtrProduct.Close();
            conn.Close();
        }
    }
}